import {getShopDetail,creatchat,shopMessage} from '../../utils/api'
Page({
  data: {
    gid:'',
    state:true,
    show:false,//弹窗
    reviews:[],//留言
    product: {}, // 商品数据
    content:''//留言内容
  },
  // 关闭弹窗
  onClose(){
    this.setData({
      show:false
    })
  },
  open(){
    this.setData({
      show:true,
      content:''
    })
  },
  //获取输入的留言
  changesend(e){
    this.setData({
      content:e.detail.value
    })
  },

  //发送留言
  async send(){
    const {gid,content} = this.data
    const {data:res} = await shopMessage({
      gid, 
      content
    })
    console.log(res)
    if(res.data.code !=200) return 
    this.getProductDetail(this.data.gid)
    this.setData({
      show:false
    })
  },

  onLoad: function (options) {
    console.log(options)
    this.setData({
      gid:options.id
    })
    // 获取商品详情数据
    this.getProductDetail(options.id);
  },
  //获取商品详细信息
  async getProductDetail (productId) {
    const {data:res} = await getShopDetail(productId)
    console.log(res)
    if(res.data.code == 200){
      if(res.data.data){
        res.data.data.url = JSON.parse(res.data.data.url)
        this.setData({
          product:res.data.data,
          reviews:res.data.review
        });
      }else{
        this.setData({
          state:false
        })
      }
  
    }
    
  },
  async Goconsult(){
    const {product:{uid},gid} = this.data
    console.log(uid,gid)
    const {data:res} = await creatchat({uid,gid})
    if(res.data.code == 200){
      wx.navigateTo({
        url: `/pages/newsinfo/newsinfo?otherid=${uid}&&id=${res.data.data.id}&&gid=${gid}`,
      })
    }
  }

});